<?php
namespace lol;


class LolRecord extends auth{

    protected $UserLolRankUrl;//用户战绩信息接口url
    protected $RecentlyHeroUrl;//最近常用的英雄url
    protected $GameRecordUrl;//最近比赛数据url
    private   $UserId;//用户userid
    private   $server_id;//服务器简写用于url拼接
    public    $UserLolData;//lol战绩概况
    protected static $instance;

    /**
     * __construct
     * @param mixed $username 游戏角色名称
     * @param mixed $servername 游戏服务器名称
     * @return mixed
     */
    public function __construct($username,$servername){
        parent::__construct();

        if(empty($username) || empty($servername)) return false;
        $AutoResult = $this->Auto($username,$servername);

        //获取id
        if(empty($AutoResult)) return false;
        $this->UserId = $AutoResult->user_id;
        $this->server_id = $AutoResult->game_zone->pinyin;
        $this->UserLolData = $AutoResult;
        if(!empty($this->UserId)){
            //获取排位信息
            $this->UserLolData = $this->getLolRank();
        }

    }

    public static function instance($username,$servername)
    {
        if (is_null(self::$instance))
        {
            self::$instance = new LolRecord($username,$servername);
        }

        return self::$instance;
    }

    /**
     * Auto 获取用户游戏id
     * @param mixed $username
     * @param mixed $servername
     * @return mixed
     */
    public function Auto($username,$servername){
        //获取服务器名称
        $servername = $this->getServerName($servername);
        if(empty($servername)) return false;
        $newtime = time();
        $key_arr = array(
            'game_zone'=>urlencode($servername),
            'player_name_list'=>urlencode($username),
            'callback'=>'jQuery11120207980825856787_'.$newtime,
            '_'=>$newtime
        );
        //拼接获取用户id接口url
        $userdata_url = $this->combineUrl(self::GET_USERID_RUL,$key_arr);
        $LolRecord = $this->get_contents($userdata_url);
        return $LolRecord->player_list[0];
    }

    /**
     * getLolRank 获取排位赛及其它比赛数据
     * @return mixed
     */
    public function getLolRank(){
        $this->UserLolRankUrl = 'http://api.lolbox.duowan.com/api/v3/player/'.$this->server_id.'/'.$this->UserId.'/';
        $newtime = time();
        $key_arr = array(
            'callback'=>'jQuery11120207980825856787_'.$newtime,
            'ts'=>'',
            '_'=>$newtime
        );
        //请求
        $url = $this->combineUrl($this->UserLolRankUrl,$key_arr);
        $data = $this->get_contents($url);
        if(empty($data)) return false;
        return $data->player_list[0];
    }

    /**
     * getLolGames 获取最近的比赛信息
     * @return mixed
     */
    public function getLolGames(){
        $this->GameRecordUrl = 'http://api.lolbox.duowan.com/api/v3/player/'.$this->server_id.'/'.$this->UserId.'/game_recent/';
        $newtime = time();
        $key_arr = array(
            'callback'=>'jQuery11120207980825856787_'.$newtime,
            'ts'=>'',
            '_'=>$newtime
        );
        //请求
        $url = $this->combineUrl($this->GameRecordUrl,$key_arr);
        $data = $this->get_contents($url);

        if(empty($data)) return false;
        return $data->game_list;
    }
}



?>